package com.hr.controller;

import com.hr.config.VerificationCode;
import com.hr.dto.JsonResp;
import com.hr.model.Menu;
import com.hr.service.CompanyService;
import com.hr.service.MenuService;
import com.hr.service.UserService;
import com.hr.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * todo
 *
 */
@RestController
public class BasicController
{
    @Autowired
    MenuService menuService;
    @Autowired
    CompanyService companyService;
    @Autowired
    UserService userService;

    @RequestMapping(value = "/getMenu",method = RequestMethod.GET)
    public ResponseEntity<JsonResp> getMenu(@RequestParam int cid)
    {
        List<Menu> menus=menuService.getMenusByCId(cid);//在service层已经进行了权限判断

        return new ResponseEntity(JsonResp.ok("menus",menus), HttpStatus.OK);
    }



    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse resp) throws IOException
    {
        VerificationCode code = new VerificationCode();
        BufferedImage image = code.getImage();
        String text = code.getText();
        HttpSession session = request.getSession(true);
        session.setAttribute("verify_code", text);
        VerificationCode.output(image,resp.getOutputStream());
    }

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public ResponseEntity<JsonResp> login() {
        return new ResponseEntity<>(JsonResp.badRequest("尚未登录，请登录!"),HttpStatus.BAD_REQUEST) ;
    }

    @RequestMapping(value = "/companys/get",method = RequestMethod.GET)
    public ResponseEntity<JsonResp> getCompanysByUser()
    {
        return new ResponseEntity<>(JsonResp.ok("companys", companyService.getCompanysByUser()),HttpStatus.OK);
    }


    @RequestMapping(value = "/company/get",method = RequestMethod.GET)
    public ResponseEntity<JsonResp> getCompanyByCId(@RequestParam int cid)
    {
        return new ResponseEntity<>(JsonResp.ok("company", companyService.getCompanyByCId(cid)),HttpStatus.OK);
    }


    @RequestMapping(value = "/system/config/menu",method = RequestMethod.GET)
    public JsonResp getMenuBak() {
        List<Menu> menus=menuService.getAllMenus();

        JsonResp jsonResp=JsonResp.ok("menus",menus);
        return jsonResp;
    }


    @RequestMapping(value = "/pw/put", method = RequestMethod.PUT)
    public ResponseEntity<JsonResp> updatePW(@RequestBody Map<String, Object> info) throws Exception
    {
        String oldpass = (String) info.get("oldpass");
        String pass = (String) info.get("pass");
        int uid = AuthUtil.getCurUser().getId();
        userService.updatePW(uid, oldpass, pass);

        return new ResponseEntity<>(JsonResp.ok("修改成功"),HttpStatus.OK);
    }

    @RequestMapping(value = "/registry/post", method = RequestMethod.POST)
    public ResponseEntity<JsonResp> registry(@RequestBody Map<String, Object> info) throws Exception
    {
        String pw = (String) info.get("password");
        String account = (String) info.get("account");
        String identity = (String) info.get("identity");
        userService.registry(account, pw, identity);

        return new ResponseEntity<>(JsonResp.ok("注册成功"),HttpStatus.OK);
    }



}
